﻿@{
    ViewBag.Title = "模块权限";
    Layout = null;
}

<style type="text/css">
    input[type="checkbox"] {
        border: 1px solid #C6C6C6;
        outline: none;
    }

    .tree-container {
        position: absolute;
        top: 10px;
        bottom: 10px;
        left: 10px;
        right: 10px;
        border: solid 1px #C8C8C8;
        overflow: auto;
    }

    .modal-body {
        padding: 0px;
    }
</style>
<div class="modal-body">
    <div class="box-body">
        <div class="col-md-12">
            <div class="box box-primary box-solid">
                <div class="box-header with-border">
                    <h3 class="box-title">请选择模块</h3>
                    <div class="box-tools pull-right">
                        <a href="javascript:void(0);" onclick=" arrowin(); return false; " data-toggle="tooltip" title="展开" class="btn btn-default btn-sm"><i class="fa fa-folder-open" id="arrowin">展开</i></a>
                        <a href="javascript:void(0);" onclick=" selectall(); return false; " data-toggle="tooltip" title="全选" class="btn btn-default btn-sm"><i class="fa fa-check-circle-o" id="select">全选</i></a>
                    </div>
                </div>
                <div class="box-body">
                    <ul id="menuTree" class="ztree" style="height: 400px; overflow: auto"></ul>
                </div>
            </div>

        </div>
    </div>
    <div class="box-footer text-right">
        <!--以下两种方式提交验证,根据所需选择-->
        <button onclick=" formSubmit(); return false; " type="button" class="btn btn-success" data-btn-type="save" title="确定"><span class="bootstrap-dialog-button-icon fa fa-check"></span>确定</button>
        <button type="button" class="btn btn-danger" data-btn-type="cancel" data-dismiss="modal"><span class="bootstrap-dialog-button-icon fa fa-times"></span>取消</button>
    </div>
    @Html.Hidden("privilegeMasterValue")
    @Html.Hidden("privilegeMaster")
</div>
<script>
    $(function () {
        initMenuTreeData();
    });
    var menuZNodes, //菜单树
        menuTreeObj, //菜单树对象
        menuSetting; //菜单设置

    //初始化菜单
    function initMenuTree() {
        //配置
        menuSetting = {
            check: {
                enable: true
            },
            view: {
                dblClickExpand: false,
                showLine: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onClick: function (e, treeId, treeNode) {
                    if (treeNode.isParent) {
                        var zTree = $.fn.zTree.getZTreeObj("menuTree");
                        zTree.expandNode(treeNode);
                    }
                }
            }
        };
        menuTreeObj = $.fn.zTree.init($("#menuTree"), menuSetting, menuZNodes);
    }

    //初始化树结构
    function initMenuTreeData() {
        UtilAjaxPost("/System/Menu/GetHaveMenuPermissionMenu", null, function (data) {
            menuZNodes = data;
            initMenuTree();
            initRoleMenuTreeData();

        });
    }

    //根据Id绑定已有权限信息
    function initRoleMenuTreeData() {
        var treeObj = $.fn.zTree.getZTreeObj("menuTree");
        //取消选中父节点之后选中子节点
        treeObj.setting.check.chkboxType = { "Y": "p", "N": "ps" };
        UtilAjaxPost("/System/Permission/GetPermissionByPrivilegeMasterValue", {
            privilegeMasterValue: $("#privilegeMasterValue").val(),
            privilegeMaster: $("#privilegeMaster").val(),
            PrivilegeAccess: Language.privilegeAccess.menu //模块按钮
        }, function (data) {
            var zTree = $.fn.zTree.getZTreeObj("menuTree");
            $.each(data, function (i, item) {
                ZtreeAssignCheck(zTree, item.PrivilegeAccessValue);
            });
            //勾选父节点后子节点也选中
            treeObj.setting.check.chkboxType = { "Y": "ps", "N": "ps" };
        });

    }

    //折叠/展开
    var expand = false;

    function arrowin() {
        if (expand) {
            menuTreeObj.expandAll(false);
            expand = false;
            $("#arrowin").html("展开").attr("class", "fa fa-folder-open");
            $("#arrowin").parent().attr("title", "展开");
        } else {
            menuTreeObj.expandAll(true);
            expand = true;
            $("#arrowin").html("折叠").attr("class", "fa fa-folder");
            $("#arrowin").parent().attr("title", "折叠");
        }
    }

    //全选/重置
    var checkAll = true;

    function selectall() {
        if (checkAll) {
            menuTreeObj.checkAllNodes(true);
            checkAll = false;
            $("#select").html("反选").attr("class", "fa fa-circle-thin");
            $("#select").parent().attr("title", "反选");
        } else {
            menuTreeObj.checkAllNodes(false);
            checkAll = true;
            $("#select").html("全选").attr("class", "fa fa-check-circle-o");
            $("#select").parent().attr("title", "全选");
        }
    }

    //表单提交
    function formSubmit() {
        DialogTipsMsgWait("正在处理中");
        var json = "";
        var nodes = menuTreeObj.getCheckedNodes(true);
        for (var i = 0; i < nodes.length; i++) {
            var info = nodes[i].id;
            json += "{\"P\":\"" + info + "\"},";
        };
        json = json.substring(0, json.length - 1);
        json = "[" + json + "]";

        UtilAjaxPostWait("/System/Permission/SavePermission",
        {
            privilegeAccess: Language.privilegeAccess.menu, //菜单
            menuPermissions: json,
            privilegeMasterValue: $("#privilegeMasterValue").val(),
            privilegeMaster:$("#privilegeMaster").val()
        }, function (data) {
            DialogAjaxResult(data);
        });
    }
</script>
